﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityModel;
using BusinessLayer;
namespace PL___Windows_Inteface
{
    public partial class ManagePrivate : Form
    {
        private Private p;
        public ManagePrivate(String operation, string username)
        {
            if (operation == "ADD")
            {
                InitializeComponent();
                sex_cb.Items.Add("Male");
                sex_cb.Items.Add("Female");
                edit.Visible = false;
                delete_bt.Visible = false;
                
                
            }
            else if (operation == "VIEW") {
                InitializeComponent();
                sex_cb.Items.Add("Male");
                sex_cb.Items.Add("Female");
                BL controller = new BL();
                p = controller.getPrivateCustomer(username);
                username_tb.Text = p.Customer.username;
                password_tb.Text = p.Customer.password;
                cpr_tb.Text = p.cpr.ToString();
                name_tb.Text = p.name;
                address_tb.Text = p.Customer.address_c;
                phone_tb.Text = p.Customer.phone.ToString();
                age_tb.Text = p.age.ToString();
                sex_cb.Text = p.sex.ToString();
                buttonSubmit.Visible = false;

            }
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            if (bt.Text == "Submit")
            {
                p = new Private();
                p.Customer = new Customer();
                p.Customer.typeCustumer = 1; 
            }
            try{
                try
                {
                    p.cpr = Convert.ToInt32(cpr_tb.Text);
                }
                catch {
                    throw new Exception("CPR only have numbers");
                }
                p.name = name_tb.Text;
                 try
                {
                    p.age = Convert.ToInt32(age_tb.Text);
                }
                catch {
                    throw new Exception("Age only have numbers");
                }
                 p.sex = sex_cb.SelectedItem.ToString();
                 p.Customer.address_c = address_tb.Text;
                 try
                {
                    p.Customer.phone = Convert.ToInt32(phone_tb.Text);
                }
                catch {
                    throw new Exception("Telephone only have numbers");
                }
                 p.Customer.username = username_tb.Text;
                 p.Customer.password = password_tb.Text;
                 BL controller = new BL();
                 if (bt.Text == "Submit")
                 {
                     controller.savePrivateCustomer(p);
                     error_lb.ForeColor = Color.Blue;
                     error_lb.Text = "Customer Added Correctly";
                 }
                 else {
                     controller.updatePrivateCustomer(p);
                     error_lb.ForeColor = Color.Blue;
                     error_lb.Text = "Customer Updated Correctly";                 
                 }

              
                 

               
                
            }catch (Exception ex){
                error_lb.Text =ex.Message;
            }
            
        }

        private void delete_bt_Click(object sender, EventArgs e)
        {
            BL controller = new BL();
            try
            {
                controller.deletePrivateCustomer(p);
                MessageBox.Show("Customer Deleted Correctly", "Deleted", MessageBoxButtons.OK);
                this.Close();
            }
            catch (Exception ex){
                error_lb.Text = ex.Message;
                
            }

        }
    }
}
